package com.situ.company.controller;


import com.situ.company.service.Impl.UserService;
import com.situ.company.service.Impl.service.Impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/login.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //校验是否正确
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //派service校验 方法由service执行
        UserService service = new UserServiceImpl();
        boolean b =service.check(username,password);
        if(b){
            //用户名密码正确，正常登录后 建立session， 把用户名打个标记
            req.getSession().setAttribute("currentUser",username);//获取哪个管 在当前数据起个名字带个数据
            //换页面重定向到首页
            resp.sendRedirect("/index");
        }else {
            req.setAttribute("errorMsg","用户名或密码错误");
            //密码错误不换页面请求转发重新登录
            req.getRequestDispatcher("/WEB-INF/login.jsp").forward(req,resp);
        }



        //四大作用域
        //page页面级别 request请求级别 session application最大级
        //el表达式获取
    }
}
